<?php 
namespace app\index\controller;
use \think\Controller;
use \think\Request;
use \think\Session;
/**
 * 前台预定流程(筛选资源)
 */
class Filter extends Controller {
	use \app\common\traits\controller\TraitController;
    //前置操作 除去出租判断
    protected $beforeActionList = [
        'checkLogin' =>['except' => 'getrelist,getrelistform,postajaxrelist3,postajaxrelist4'],
    ];
    //判断是否登陆
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect(url('/user/login','','','user'));
            exit;
        }
    }
    /**
     * [getfilter 筛选界面]房车
     * @return [type] [description]
     */
    public function getExList()
    { 
        //$caravan_id = 2; $take_car_date=2016-12-22; $leave_car_date=2016-12-24;
        //$a = model('Calendar')->ordered($caravan_id, $take_car_date, $leave_car_date);
        // $number = '114823121632451';
        //$a = model('Orders')->cancelOrder($number);halt($a);
        //城市列表
        $city = model('Filters')->getCity();
        $this->assign('city',$city);
        $integral = $this->getLoginUserInfo('integral');
        $this->assign('integral',$integral);
        return view('filter/exlist',['list1'=>'','list2'=>'','s'=>'','e'=>'','a'=>'','b'=>'','k'=>'']);
    }
    /**
     * [postExListForm 筛选界面表单]
     * @return [type] [description]
     */
    public function getExListForm()
    {
        $this->getCheckFilterAble();
        $filter_info['sdate'] = input('get.sdate');$filter_info['edate'] = input('get.edate');
        $filter_info['zone'] = input('get.city'); $k = input('get.city');
        //获取首页数据
        if (!empty(input('sdate'))) {
            $filter_info['sdate'] = input('sdate');
        }
        if (!empty(input('edate'))) {
            $filter_info['edate'] = input('edate');
        }
        if (!empty(input('city'))) {
           $filter_info['zone'] = input('city');
        }
        $s =  $filter_info['sdate']; $e = $filter_info['edate'];
        $list1 = model('Filters')->getList1($filter_info,1);//halt($list1);
        $list2 = model('Filters')->getList2($filter_info,1);
        $cates = db('car_cates')->column('name', 'id');
        //城市列表
        $city = model('Filters')->getCity();
        $this->assign('city',$city);
        $integral = $this->getLoginUserInfo('integral');
        $this->assign('integral',$integral);
        return view('filter/exlist',['list1'=>$list1,'list2'=>$list2,'s'=>$s,'e'=>$e,'a'=>json_encode($cates),'b'=>json_encode(['1'=>'有','2'=>'无']),'k'=>$k]);
    }
    /**
     * [postAjaxList2 加载列表2]
     * @return [type] [description]
     */
    public function postAjaxList2()
    {
        $filter_info = input('filter_info/a');
        $page = input('page', 2);
        $list2 = model('Filters')->getList2($filter_info,$page);
        return json($list2);
    }
    /**
     * [postAjaxList1 加载列表1]
     * @return [type] [description]
     */
    public function postAjaxList1()
    {
        $filter_info = input('filter_info/a');
        $page = input('page', 2);
        $list1 = model('Filters')->getList1($filter_info,$page);
        return json($list1);
    }
    /**
     * [getCheckFilterAble 判断是否有权搜索交换列表]
     * @return [type] [description]
     */
    public function getCheckFilterAble()
    {
        //信息完善
        $bt1 = model('Users')->getLoginUserInfo('real_name');
        $bt2 = model('Users')->getLoginUserInfo('email');
        if (empty($bt1)||empty($bt2)) {
            //跳转个人资料完善
            $this->error('请完善个人资料必填项',url('/userinfo/personinfo','','','user'));
            
        }
        //房车通过1为通过//-99拒绝审核<------>0正在审核
        $user_type = model('Users')->getLoginUserInfo('status');
        if ($user_type<>1) {
            //跳转房车资料界面
            $this->error('您的房车未审核通过,请重新提交审核',url('/caravaninfo/index','','','user'));
          
        }
        $user_id = session('user.id');
        $btx = model('CaravanInfos')->where('user_id',$user_id)->value('registration');
        if ($user_type===0&&$btx) {
            //跳转正在审核通过界面
            $this->error('您的房车正在审核',url('caravanaudit/waitcheck','','','user'));
          
        }
        //旅行币小于基数
        $now_integral = $this->getLoginUserInfo('integral');
        $camp_id = $this->getLoginUserInfo('camp_id');
        $travel_money_base = model('Camps')->where('id',$camp_id)->value('travel_money_base');
        if ($now_integral<$travel_money_base) {
            //跳转界面
            $this->redirect('/filter/jumperror');
        }
    }
    /**
     * [getJumpError 错误界面]
     * @return [type] [description]
     */
    public function getJumpError()
    {
        return view('filter/cashlowtanchu');
    }
    /**
     * [getCheckIntegralEnough 基数判断与弹框]
     * @return [type] [description]
     */
    public function getCheckIntegralEnough()
    {
        //旅行币小于基数
        $now_integral = $this->getLoginUserInfo('integral');
        $camp_id = $this->getLoginUserInfo('camp_id');
        $travel_money_base = model('Camps')->where('id',$camp_id)->value('travel_money_base');
        if ($now_integral<$travel_money_base) {
            Session::set('redirect_url', url("/filter/exlist",'','','www'));
            //触发弹出框
            return json(0);
        }
        return 1;
       
    }

    //////////////////////////出租流程////////////////////////////
    /**
     * [getRelist 出租筛选列表]
     * @return [type] [description]
     */
    public function getRelist()
    {
        //城市列表
        $city = model('Filters')->getCity();
        $this->assign('city',$city);
        return view('filter/rentlist',['list1'=>'','list2'=>'','s'=>'','e'=>'','a'=>'','b'=>'','k'=>'']);
    }

    /**
     * [postRelistForm 筛选界面表单]
     * @return [type] [description]
     */
    public function getRelistForm()
    {
        //获取首页数据
        if (!empty(input('sdate'))) {
            $filter_info['sdate'] = input('sdate');
        }
        if (!empty(input('edate'))) {
            $filter_info['edate'] = input('edate');
        }
        if (!empty(input('city'))) {
           $filter_info['zone'] = input('city');
        }
        $filter_info['sdate'] = input('get.sdate');$filter_info['edate'] = input('get.edate');
        $filter_info['zone'] = input('get.city'); $k = input('get.city');
        $s = $filter_info['sdate']; $e = $filter_info['edate'];
        $list1 = model('Filters')->getList3($filter_info,1);//halt($list1);
        $list2 = model('Filters')->getList4($filter_info,1);
        //c 为日历表联合 c.c即最低价格
        foreach ($list1 as $key => $value) {
            $value->c = model('Calendar')->where('caravan_id',$value->caravan_id)->where('datetimes', 'between', [$value->s, $value->e])->field("*,min(single_day_price)as c")->group('id')->find();
           
        }
        foreach ($list2 as $key => $value) {
            $value->c = model('Calendar')->where('caravan_id',$value->caravan_id)->where('datetimes', 'between', [$value->s, $value->e])->field("*,min(single_day_price)as c")->group('id')->find();
        }
        $cates = db('car_cates')->column('name', 'id');
        //城市列表
        $city = model('Filters')->getCity();
        $this->assign('city',$city);
        return view('filter/rentlist',['list1'=>$list1,'list2'=>$list2,'s'=>$s,'e'=>$e,'a'=>json_encode($cates),'b'=>json_encode(['1'=>'有','2'=>'无']),'k'=>$k]);
    }
    /**
     * [postAjaxRelist3 出租加载列表3]
     * @return [type] [description]
     */
    public function postAjaxRelist3()
    {
        $filter_info = input('filter_info/a');
        $page = input('page', 2);
        $list2 = model('Filters')->getList3($filter_info,$page);
        foreach ($list2 as $key => $value) {
            $value->c = model('Calendar')->where('caravan_id',$value->caravan_id)->where('datetimes', 'between', [$value->s, $value->e])->field("*,min(single_day_price)as c")->group('id')->order('c asc')->find();
        }
        return json($list2);
    }
    /**
     * [postAjaxRelist4 出租加载列表4]
     * @return [type] [description]
     */
    public function postAjaxRelist4()
    {
        $filter_info = input('filter_info/a');
        $page = input('page', 2);
        $list1 = model('Filters')->getList4($filter_info,$page);
        foreach ($list1 as $key => $value) {
            $value->c = model('Calendar')->where('caravan_id',$value->caravan_id)->where('datetimes', 'between', [$value->s, $value->e])->field("*,min(single_day_price)as c")->group('id')->order('c asc')->find();
        }
        return json($list1);
    }
}